package com.example.innovate.controller;

import com.example.innovate.bean.Result;
import com.example.innovate.domain.AdminInfoDO;
import com.example.innovate.domain.JobExamineDO;
import com.example.innovate.domain.JobWantedDO;
import com.example.innovate.service.AdminService;
import com.example.innovate.service.JobExamineService;
import com.example.innovate.service.JobWantedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/jobWanted")
@Api(tags = "求职信息表接口")
public class JobWantedController {
    @Autowired
    JobWantedService jobWantedService;
    @Autowired
    AdminService adminService;
    @Autowired
    JobExamineService jobExamineService;

    @PostMapping("/addJobWanted")
    @ApiOperation("添加求职信息")
    public Result addJobWanted(@RequestBody JobWantedDO jobWantedDO,
                            @RequestParam("userId") Integer userId) throws Exception{
        if(userId!=0){
            jobWantedDO.setJobUserId(userId);
        }
        jobWantedService.addInfo(jobWantedDO);
        JobWantedDO jobWantedDO1 = jobWantedService.findByName(jobWantedDO.getJobName());
        JobExamineDO jobExamineDO = new JobExamineDO();
        if(jobWantedDO1!=null&&jobWantedDO1.getJobId()!=null){
            jobExamineDO.setExWantedId(jobWantedDO1.getJobId());
            jobExamineDO.setExState("待审核");
            jobExamineService.addInfo(jobExamineDO);
        }
        return new Result(200,"success","添加成功",jobWantedDO);
    }

    @DeleteMapping("/deleteJobWanted")
    @ApiOperation("删除求职信息")
    public Result deleteJobWanted(@RequestParam("id") int id,
                               @RequestParam("adminUserName") String adminUserName) throws Exception{
        JobWantedDO jobWantedDO = jobWantedService.findById(id);
        AdminInfoDO adminInfoDO = adminService.getAdminInfoByName(adminUserName);
        if(jobWantedDO!=null){
            if(adminInfoDO!=null){
                if(adminInfoDO.getAdminIsUpdate().equals(false)){
                    return new Result(400,"error","没有删除权限",jobWantedDO);
                }
            }
            jobWantedService.deleteInfo(id);
            return new Result(200,"success","删除成功",jobWantedDO);
        }
        else {
            return new Result(400,"error","信息不存在",jobWantedDO);
        }

    }

    @PostMapping("/updateJobWanted")
    @ApiOperation("修改求职信息")
    public Result updateJobWanted(@RequestBody JobWantedDO jobWantedDO,
                               @RequestParam("adminUserName") String adminUserName) throws Exception{
        JobWantedDO jobWantedDO1 = jobWantedService.findById(jobWantedDO.getJobId());
        AdminInfoDO adminInfoDO = adminService.getAdminInfoByName(adminUserName);
        if(jobWantedDO1!=null){
            if(adminInfoDO!=null){
                if(adminInfoDO.getAdminIsUpdate().equals(false)){
                    return new Result(400,"error","没有修改权限",jobWantedDO);
                }
            }
            jobWantedService.updateInfo(jobWantedDO);
            return new Result(200,"success","修改成功",jobWantedDO);
        }
        else {
            return new Result(400,"error","信息不存在",jobWantedDO);
        }
    }

    @GetMapping("/findJobWanted")
    @ApiOperation("查找求职信息")
    public Result findAllJobWanted(@RequestParam("pageNumber") Integer pageNumber,
                                   @RequestParam("pageSize") Integer pageSize,
                                   @RequestParam("userId") int userId,
                                   String graSchool,
                                   String major,
                                   String company,
                                   String position,
                                   String firstWorkPlace) throws Exception{
        List<JobWantedDO> list =
                jobWantedService.findInfo(pageNumber,pageSize,graSchool,major,company,position,firstWorkPlace,userId);
        return new Result(200,"success","查找成功",list);

    }
}
